package mydessertstore.web.servlet;

import mydessertstore.domain.Account;
import mydessertstore.domain.Cart;
import mydessertstore.domain.Order;
import mydessertstore.service.LogService;
import mydessertstore.service.OrderService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
import java.util.Objects;

import static java.lang.Integer.getInteger;
import static java.lang.Integer.valueOf;

public class ViewOrderFormServlet extends HttpServlet {

    private static final String VIEW_ORDER_FORM = "/WEB-INF/jsp/order/viewOrder.jsp";
    private static final String ERROR = "/WEB-INF/jsp/common/error.jsp";

    private OrderService orderService;
    private Order order;
    private Cart cart;
    private int orderId;
    private String page;

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        doGet(req, resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset = UTF-8");
        HttpSession session = req.getSession();
        page = (String) session.getAttribute("page");


        Account account = (Account) session.getAttribute("account");

        if(Objects.equals(page, "listOrders"))     //如果请求是从查看订单页面发出的
        {
            orderService = new OrderService();
            String orderIdStr = req.getParameter("orderId");
            orderId = Integer.parseInt(orderIdStr);
            System.out.println(orderId);

            order = orderService.getOrder(orderId);
            System.out.println(order.getShipZip());
            session.setAttribute("order", order);

            if(order != null) {
                //日志功能
                if(account!=null) {

                    LogService logService = new LogService();

                    String logInfo = logService.logInfo(" ") + " 查看历史订单 " ;
                    logService.insertLogInfo(account.getUsername(), logInfo);
                }
                else{
                    LogService logService = new LogService();
                    String logInfo = logService.logInfo(" ") + " 查看历史订单 " ;
                    logService.insertLogInfo("未登录用户", logInfo);
                }
                //日志结束

                req.getRequestDispatcher(VIEW_ORDER_FORM).forward(req, resp);
            }else {
                session.setAttribute("message",  "You may only view your own orders.");

                req.getRequestDispatcher(ERROR).forward(req, resp);
            }


        }else {     //如果请求是从confirmOrder界面发出的

            orderService = new OrderService();
            System.out.println("confirm");


            order = (Order) session.getAttribute("order");
            cart = (Cart) session.getAttribute("cart");

            if(order != null){

                //订单插入数据库
                System.out.println("begin to insert order");

                orderService.insertOrder(order);
                session.setAttribute("order", order);

                //清空购物车
                cart = null;
                session.setAttribute("cart", cart);
                System.out.println("cart is empty");

                session.setAttribute("message", "Thank you, your order has been submitted.");

                //日志功能
                if(account!=null) {

                    LogService logService = new LogService();

                    String logInfo = logService.logInfo(" ") + " 查看订单 " ;
                    logService.insertLogInfo(account.getUsername(), logInfo);
                }
                else{
                    LogService logService = new LogService();
                    String logInfo = logService.logInfo(" ") + " 查看订单 " ;
                    logService.insertLogInfo("未登录用户", logInfo);
                }
                //日志结束

                req.getRequestDispatcher(VIEW_ORDER_FORM).forward(req, resp);
            }else {
                session.setAttribute("message",  "You may only view your own orders.");

                req.getRequestDispatcher(ERROR).forward(req, resp);
            }
        }

    }
}
